package com.zhenquan.doubanread.controller.portal;

import com.github.pagehelper.PageInfo;
import com.zhenquan.doubanread.common.Const;
import com.zhenquan.doubanread.common.ResponseCode;
import com.zhenquan.doubanread.common.ServerResponse;
import com.zhenquan.doubanread.pojo.HaveRead;
import com.zhenquan.doubanread.pojo.User;
import com.zhenquan.doubanread.pojo.WantRead;
import com.zhenquan.doubanread.service.IHaveReadService;
import com.zhenquan.doubanread.service.IWantReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2018/2/3 0003.
 */
@Controller
@RequestMapping("/haveread/")
public class HaveReadController {
    @Autowired
    private IHaveReadService iHaveReadService;

    @RequestMapping(value = "add.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> add(HttpSession session,  Integer id, String title, String rating, String author, String pubdate, String origin_title, String image, String binding, String translator,
                                      String catelog, Integer pages, String image_large, String publisher, String isbn10, String isbn13, String author_intro, String summary, Double price,
                                      Integer categoryid) {
        User user = (User) session.getAttribute(Const.CURENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (id == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        HaveRead haveRead = new HaveRead(id, title, rating, author, pubdate, origin_title, image, binding, translator, catelog, pages, image_large, publisher, isbn10, isbn13, author_intro, summary, price, categoryid, user.getId());
        return iHaveReadService.addOrRemove(haveRead);
    }

    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(HttpSession session, int pageNum, int pageSize) {
        User user = (User) session.getAttribute(Const.CURENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iHaveReadService.list(user.getId(), pageNum, pageSize);
    }


}
